﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Infrastructure
{
    /// <summary>
    /// Model Mapper
    /// </summary>
    /// <typeparam name="T">需要转换的模型</typeparam>
    /// <typeparam name="S">转换后的模型</typeparam>
    public static class MapperHelper<T, S>
    {
        /// <summary>
        /// 模型转换方法
        /// </summary>
        /// <param name="cust">输入模型</param>
        /// <returns>输出模型</returns>
        public static S ToViewModel(T cust)
        {
            var config = new MapperConfiguration(cfg => { cfg.CreateMap<T, S>(); });
            AutoMapper.Mapper iMapper = (AutoMapper.Mapper)config.CreateMapper();
            var destination = iMapper.Map<T, S>(cust);
            return destination;
        }
        /// <summary>
        /// list mapper
        /// </summary>
        /// <typeparam name="T">需要转换的新类型</typeparam>
        /// <typeparam name="S">输入的旧数据类型</typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static IEnumerable<T> MapToList<T, S>(IEnumerable<T> source) where T : class where S : class
        {
            if (source == null)
            {
                return new List<T>();
            }
            var config = new MapperConfiguration(cfg => cfg.CreateMap<T, S>());
            var mapper = config.CreateMapper();
            return mapper.Map<List<T>>(source);
        }
    }
}
